package cn.com.cifi.mars.util;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class ReportUtils {

    private static final String[] NUM_BUFFER = {"零","一","二","三","四","五","六","七","八","九","十"};

    private static final String SPLIT = "/";

    public static int compareStrStartedChinaNum(String a,String b){
        if (isChinaNumStartStr(a) && isChinaNumStartStr(b)){
            return calcChinaSortNo(a) - calcChinaSortNo(b);
        }
        return a.compareTo(b);
    }

    public static boolean isChinaNumStartStr(String name){
        char[] chars = name.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            boolean flag = false;
            for (int j = 0; j < NUM_BUFFER.length; j++) {
                if (NUM_BUFFER[j].equals(String.valueOf(chars[i]))){
                    return true;
                }
            }
            if (!flag){
                return false;
            }
        }
        return false;
    }

    /**
     * 数据格式开头只能是：一、十一、九十一这种，不能出现二三、一四、或者一百五十这些
     * 支持最大九十九的大转小
     * @param name 如：一期、十一组团、八十批次
     * @return
     */
    public static int calcChinaSortNo(String name){
        int len = name.length();
        char[] chars = name.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            boolean flag = false;
            for (int j = 0; j < NUM_BUFFER.length; j++) {
                if (NUM_BUFFER[j].equals(String.valueOf(chars[i]))){
                    flag = true;
                    break;
                }
            }
            if (!flag){
                len = i;
                break;
            }
        }
        if (len == 1){
            return ArrayUtils.indexOf(NUM_BUFFER,String.valueOf(chars[len-1]));
        }
        if (len == 2){
            return ArrayUtils.indexOf(NUM_BUFFER,String.valueOf(chars[len-1])) + 10;
        }
        if (len == 3){
            return ArrayUtils.indexOf(NUM_BUFFER,String.valueOf(chars[len-1])) + 10 *  ArrayUtils.indexOf(NUM_BUFFER,String.valueOf(chars[len-3])) ;
        }
        return 0;
    }


    public static final String toString(Object o) {
        if (o == null) {
            return "";
        }
        if (o instanceof LocalDateTime) {
            return ((LocalDateTime) o).format(DateTimeFormatter.ISO_LOCAL_DATE);
        }
        return String.valueOf(o);
    }

    public static final String toStringQFW(Object o) {
        if (o == null) {
            return "";
        }
        return MathUtils.toQFW(String.valueOf(o));
    }

    public static String split(Object... objects) {
        if (objects.length == 1) {
            return toString(objects[0]);
        }
        String[] r = new String[objects.length];
        for (int i = 0; i < objects.length; i++) {
            r[i] = toString(objects[i]);
        }
        return StringUtils.join(r, SPLIT);
    }
    
    public static String bigDecimalToString(BigDecimal val) {
    	if(val == null) {
    		return "";
    	}
    	return val.toString();
    }

    public static String splitQWF(Object... objects) {
        if (objects.length == 1) {
            return toStringQFW(objects[0]);
        }
        String[] r = new String[objects.length];
        for (int i = 0; i < objects.length; i++) {
            r[i] = toStringQFW(objects[i]);
        }
        return StringUtils.join(r, SPLIT);
    }

    /**
     * 设置列值
     * @param cell
     * @param value
     */
    public static void setCellValue(Cell cell,String value){

        //像数值，但需要按文本处理的
        if (MathUtils.isNumber(value) && value.startsWith("0") && value.length() > 1 && !value.startsWith("0.")){
            cell.setCellValue(value);
            return;
        }

        //数值
        if (MathUtils.isNumber(value)){
            cell.setCellValue(Double.valueOf(value));
            return;
        }

        //手动/分隔的数值，手动加千分位
        if (StringUtils.isNotBlank(value) && value.contains("/")){
            String[] split = value.split("/");
            boolean isSplitNumber = true;
            for (String s : split) {
                if (!MathUtils.isNumber(s)){
                    isSplitNumber = false;
                    break;
                }
            }
            cell.setCellValue(isSplitNumber?ReportUtils.splitQWF(split):value);
            return;
        }

        //百分比
        if (StringUtils.isNotBlank(value) && value.endsWith("%")){
            BigDecimal bigDecimal = MathUtils.newBigDecimal(value.replace("%", ""));
            cell.setCellValue(bigDecimal.divide(MathUtils.NUM_100,bigDecimal.scale()+2, RoundingMode.HALF_UP).doubleValue());
            return;
        }

        //日期
        if (StringUtils.isNotBlank(value)  && DateUtils.isDate(value)){
            cell.setCellValue(DateUtils.toDate(value));
            return;
        }

        //其他按文本
        cell.setCellValue(value == null ? "" : value);
    }
    
    
    /**
     * 设置列值
     * @param cell
     * @param value
     */
    public static void setCellValue(XSSFCell cell,String value){

        //像数值，但需要按文本处理的
        if (MathUtils.isNumber(value) && value.startsWith("0") && value.length() > 1 && !value.startsWith("0.")){
            cell.setCellValue(value);
            return;
        }

        //数值
        if (MathUtils.isNumber(value)){
            cell.setCellValue(Double.valueOf(value));
            return;
        }

        //手动/分隔的数值，手动加千分位
        if (StringUtils.isNotBlank(value) && value.contains("/")){
            String[] split = value.split("/");
            boolean isSplitNumber = true;
            for (String s : split) {
                if (!MathUtils.isNumber(s)){
                    isSplitNumber = false;
                    break;
                }
            }
            cell.setCellValue(isSplitNumber?ReportUtils.splitQWF(split):value);
            return;
        }

        //百分比
        if (StringUtils.isNotBlank(value) && value.endsWith("%")){
            BigDecimal bigDecimal = MathUtils.newBigDecimal(value.replace("%", ""));
            cell.setCellValue(bigDecimal.divide(MathUtils.NUM_100,bigDecimal.scale()+2, RoundingMode.HALF_UP).doubleValue());
            return;
        }

        //日期
        if (StringUtils.isNotBlank(value)  && DateUtils.isDate(value)){
            cell.setCellValue(DateUtils.toDate(value));
            return;
        }

        //其他按文本
        cell.setCellValue(value == null ? "" : value);
    }
    
    /**
     * 设置列值
     * @param cell
     * @param value
     */
    public static void setXSSFCellValue(XSSFCell cell,String value){
        //数值
        if (MathUtils.isNumber(value)){
            cell.setCellValue(Double.valueOf(value));
            return;
        }

        //手动/分隔的数值，手动加千分位
        if (StringUtils.isNotBlank(value) && value.contains("/")){
            String[] split = value.split("/");
            boolean isSplitNumber = true;
            for (String s : split) {
                if (!MathUtils.isNumber(s)){
                    isSplitNumber = false;
                    break;
                }
            }
            cell.setCellValue(isSplitNumber?ReportUtils.splitQWF(split):value);
            return;
        }

        //百分比
        if (StringUtils.isNotBlank(value) && value.endsWith("%")){
        	BigDecimal bigDecimal = MathUtils.newBigDecimal(value.replace("%", ""));
            cell.setCellValue(bigDecimal.divide(MathUtils.NUM_100,bigDecimal.scale()+2, RoundingMode.HALF_UP).doubleValue());
            return;
        }

        //日期
        if (StringUtils.isNotBlank(value)  && DateUtils.isDate(value)){
            cell.setCellValue(DateUtils.toDate(value));
            return;
        }

        //其他按文本
        cell.setCellValue(value == null ? "" : value);
    }
}
